import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { IEntityDtoOfGuid, PagedTaxonomyOut, SetStatusInput, TaxonomyServiceProxy } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/api';
import { Paginator } from 'primeng/paginator';
import { Table } from 'primeng/table';
import { finalize } from 'rxjs/operators';
import { CreateOrEditTaxonomyModalComponent } from './create-or-edit-taxonomy-modal.component';
import { TaxonomyItemModalComponent } from './taxonomy-item-modal.component';

@Component({
    templateUrl: './taxonomy.component.html',
    animations: [appModuleAnimation()]
})
export class TaxonomyComponent extends AppComponentBase {

    @ViewChild('dataTable', { static: true }) dataTable: Table;
    @ViewChild('paginator', { static: true }) paginator: Paginator;
    
    @ViewChild('createOrEditTaxonomyModal', { static: true }) createOrEditTaxonomyModal: CreateOrEditTaxonomyModalComponent;
    @ViewChild('taxonomyItemModal', { static: true }) taxonomyItemModal: TaxonomyItemModalComponent;
 

    filterText = '';
    selectedDatas: IEntityDtoOfGuid[];
    constructor(
        injector: Injector,
        private _taxonomyService: TaxonomyServiceProxy) {
        super(injector);

    }

    ngOnInit() {
        this.getListData(null);
    }

    getListData(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            if (this.primengTableHelper.TryChangePage(this.paginator, 0)) {
                return;
            }
        }

        this.selectedDatas = [];

        this.primengTableHelper.showLoadingIndicator();
        this._taxonomyService.getPagedTaxonomy(
            this.filterText,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }

    goToDetail(entity:PagedTaxonomyOut){
        this.taxonomyItemModal.show(entity);
    }

    editInfo (entry:PagedTaxonomyOut) {
        this.openCreateOrEdit(entry);
    };
    createNews (){
        this.openCreateOrEdit(null);
    }

    delete(){
        if (!this.selectedDatas) return;
        let ids = this.selectedDatas.map(item=>item.id);
        if (ids.length == 0) return;
        this.message.confirm(this.l('DeleteWarningMessage'), this.l('AreYouSure'),
            (isConfirmed) => {
                if (!isConfirmed) return;
                this._taxonomyService.deleteTaxonomy(ids)
                    .subscribe(() => {
                        this.selectedDatas =[];
                        this.reloadPage();
                        this.notify.success(this.l('SuccessfullyDeleted'));
                    });
            })
    }

    reloadPage() {
        if (this.primengTableHelper.TryChangePage(this.paginator, this.paginator.getPage())) {
            return;
        }

        this.getListData(null);
    }

   
    changeState(state: boolean) {
        if (!this.selectedDatas) return;
        let ids = this.selectedDatas.map(item=>item.id);
        
        if (ids.length > 0) {
            let msg: string;
            if (state) {
                msg = this.l('ReleaseWarningMessage');
            }
            else {
                msg = this.l('UndoReleaseWarningMessage');
            }
            this.message.confirm(msg, this.l('AreYouSure'),
                (isConfirmed) => {
                    if (!isConfirmed) return;

                    let input = new SetStatusInput();
                    input.idList = ids;
                    input.isActive = state;

                    this._taxonomyService.setTaxonomyStatus(input)
                        .subscribe(() => {
                            this.selectedDatas =[];
                            this.getListData(null);
                            if (state) {
                                this.notify.success(this.l('ReleaseSuccessfully'));
                            } else {
                                this.notify.success(this.l('CancelReleaseSuccessfully'));
                            }
                        });
                })
        }
    }

    openCreateOrEdit(entry:PagedTaxonomyOut) {
        this.createOrEditTaxonomyModal.show(entry);
    }
}
